package com.wie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwb.util.Message;
import com.gwb.util.MoonshotAiUtils;
import com.gwb.util.RoleEnum;
import com.wie.domain.dto.CountryGdpDTO;
import com.wie.domain.dto.CountryGdpListDTO;
import com.wie.domain.entity.CountryGdp;
import com.wie.domain.entity.ProvinceGdp;
import com.wie.domain.vo.CountryGdpListVO;
import com.wie.domain.vo.CountryGdpVO;
import com.wie.domain.vo.ProvinceGdpVO;
import com.wie.service.CountryGdpService;
import com.wie.mapper.CountryGdpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
* @author WiedersehenM
* @description 针对表【country_gdp(某国gdp)】的数据库操作Service实现
* @createDate 2024-10-20 17:18:37
*/
@Service
public class CountryGdpServiceImpl extends ServiceImpl<CountryGdpMapper, CountryGdp>
    implements CountryGdpService{

    @Autowired
    private CountryGdpMapper countryGdpMapper;

    /**
     * 获取某一年某国家的gdp
     * @param countryGdpDTO
     * @return
     */
    @Transactional
    public CountryGdpVO getCountryGdpData(CountryGdpDTO countryGdpDTO) {
        // 1. 在数据库中查询有没有需要的值
        String country = countryGdpDTO.getCountry();
        String year = countryGdpDTO.getYear();
        HashMap<String, Object> wrapperMap = new HashMap<>();
        wrapperMap.put("country", country);
        wrapperMap.put("year", year);
        List<CountryGdp> countryGdpDB = countryGdpMapper.selectByMap(wrapperMap);
        if(countryGdpDB.size() != 0) {
            CountryGdp countryGdp = countryGdpDB.get(0);
            CountryGdpVO countryGdpVO = CountryGdpVO.builder()
                    .country(country)
                    .year(year)
                    .gdp(countryGdp.getGdp())
                    .build();
            return countryGdpVO;
        }
        // 2. 调用ai接口查询
        // 3. 指定查询信息
        String message = country + year + "年gdp(单位为：亿美元)" + ",仅回答数字，请用“国家的名称...年的gdp为...亿“这种格式回答，如果你的答案是\"...万亿美元，\"请将万亿美元的单位换为亿";
        List<Message> messages = CollUtil.newArrayList(
                new Message(RoleEnum.system.name(), message)
        );
        MoonshotAiUtils moonshotAiUtils = new MoonshotAiUtils();
        String result = moonshotAiUtils.doChat("moonshot-v1-8k", messages);
        // 4. 截取有效信息
        int beginIndex = result.indexOf("为");
        beginIndex += 1;
        int endIndex = result.indexOf("亿") + 1;
        String gdp = result.substring(beginIndex, endIndex);
        // 封装GdpData, 将数据存入数据库
        CountryGdp countryGdp = new CountryGdp().builder()
                .country(country)
                .year(year)
                .gdp(gdp)
                .create_time(LocalDateTime.now())
                .build();
        countryGdpMapper.insert(countryGdp);
        // 封装GdpDataVO，并返回
        CountryGdpVO countryGdpVO = CountryGdpVO.builder()
                .country(country)
                .year(year)
                .gdp(gdp)
                .build();
        return countryGdpVO;
    }

}




